package com.xinke.edu.mypetadoption.fragment2.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.xinke.edu.mypetadoption.entity.ProductCategory;

import java.util.List;

public class CategoryAdapter extends ArrayAdapter<ProductCategory> {

    private Context mContext;
    private List<ProductCategory> mCategoryList;

    public CategoryAdapter(Context context, List<ProductCategory> categoryList) {
        super(context, 0, categoryList);
        mContext = context;
        mCategoryList = categoryList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    // 自定义显示布局
    private View getCustomView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext)
                    .inflate(android.R.layout.simple_spinner_item, parent, false);
        }

        TextView textView = convertView.findViewById(android.R.id.text1);
        // 设置显示分类名称
        textView.setText(mCategoryList.get(position).getName());

        return convertView;
    }
}
